package kr.couponplay.android.activity.deal;

import kr.couponplay.android.activity.R;
import kr.couponplay.android.domain.deal.Deal;
import kr.couponplay.android.support.DisplayUtils;
import kr.couponplay.android.support.StringUtils;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class DealDetailActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.deal_detail_activity);

		WebView image = (WebView)findViewById(R.id.deal_image);
		image.setFocusable(false);
		LayoutParams layoutParams = image.getLayoutParams();
		int width = DisplayUtils.widthPixels(this);
		layoutParams.width = width;
		layoutParams.height = (int)(width * 0.65);
		image.setLayoutParams(layoutParams);
		
		TextView title = (TextView) findViewById(R.id.detail_title);
		TextView originalPrice = (TextView) findViewById(R.id.detail_original_price);
		TextView salesPrice = (TextView) findViewById(R.id.detail_sales_price);
		TextView discountRate = (TextView) findViewById(R.id.detail_discount_rate);
		TextView nowCount = (TextView) findViewById(R.id.detail_now_count);
		TextView saleToDate = (TextView) findViewById(R.id.detail_sale_to_date);
		TextView providerName = (TextView) findViewById(R.id.detail_provider_name);
					
		final Deal deal = (Deal)getIntent().getSerializableExtra("deal");
		
		if (image != null) {
			StringBuffer sb = new StringBuffer();
			sb.append("<html><body style='padding:0;margin:0;'>");
			sb.append("<img src='" + deal.getThumbnailImageUrl2() + "' style='width:100%;padding:0;margin:0' />");
			sb.append("</body></html>");
			image.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
		}

		
		title.setText(deal.display1());
		originalPrice.setText(DisplayUtils.numFormat(deal.getOriginalPrice()) + "원");
		originalPrice.setPaintFlags(originalPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
		salesPrice.setText(DisplayUtils.numFormat(deal.getSalesPrice()) + "원");
		discountRate.setText(deal.getDiscountRate() + "%");
		nowCount.setText(DisplayUtils.numFormat(deal.getNowCount()) + "개");
		saleToDate.setText(DisplayUtils.dateFormat(Long.parseLong(deal.getToDate())) + " 까지");
		providerName.setText(deal.getProvider().getName());
		
		TextView joinExplain = (TextView)findViewById(R.id.join_explain);
		Button joinButton = (Button)findViewById(R.id.join_button);
		
		if (StringUtils.isEmpty(deal.getProvider().getJoinUrl())) {
			joinExplain.setVisibility(View.INVISIBLE);
			joinButton.setVisibility(View.INVISIBLE);
		} else {
			joinExplain.setVisibility(View.VISIBLE);
			joinExplain.setText("아직도 " + deal.getProvider().getName() + " 회원이 아니세요?");

			joinButton.setVisibility(View.VISIBLE);
			joinButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Uri uri = Uri.parse(deal.getProvider().getJoinUrl());
					startActivity(new Intent(Intent.ACTION_VIEW, uri));
				}
			});
		}
        
        Button detailButton = (Button)findViewById(R.id.detail_button);
        detailButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Uri uri = Uri.parse(deal.getDetailViewUrl());
        		startActivity(new Intent(Intent.ACTION_VIEW, uri));
        	}
        });
    }
}
